/** 武装飞鸟1,游聚用,从战国ace改过来,
  增加了手动蓄力功能,也就是用热键模拟按住攻击键,避免手动蓄力与连发发生冲突
 
  By setycyas @2025-02-27
*/

/* 全局设定
*/ {
#Warn
#MaxThreadsPerHotkey 1
SetWorkingDir(A_ScriptDir)
#Include "..\Class\FaceDetector.2.ahk"
#Include "..\Class\CommonMacro.2.ahk"
#Include "..\Class\CommonUI.2.ahk"
#Include "..\Class\MyFuns.2.ahk"
#Include "..\Class\MyEmu.2.ahk"
#Include "..\Class\OrderMap.2.ahk"
#Include "..\Class\DrawWindow.2.ahk"
;SendMode("Event")
SendMode("Input")
}

/* 制作武装飞鸟1的宏类
*/ class Gunbird1Macro extends CommonMacro {

  pressDuration := 30
  aFlag := False ;a键按下与否的标记

  /* 函数
  */
  ;需要蓄力delay时间的蓄力攻击
  _蓄力攻击(delay, key){
    local vk
    if (this.aFlag) {
      this._up(1)
      sleep(30)
      this.aFlag := True
    }
    this._down(1)
    this.aFlag := True
    sleep(delay)
    this._up(1)
    this.aFlag := False
    ;仍按住键则启用连发
    vk := GetKeyVK(MyFuns.getHotkey(key))
    if (MyFuns.getVkState(vk)) {
      this._连发A(key)
    }
  }

  /* 新增的宏函数
  */
  ;改装一下连发A,当被蓄力中断后判断一下按键的放开状态
  _连发A(key){
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    while (MyFuns.getVkState(vk)) {
      this._down(1)
      this.aFlag := True
      sleep(this.pressDuration)
      if (this.aFlag) {
        this._up(1)
        this.aFlag := False
      }
      sleep(this.autoFireDelay)
    }
  }
  ;在按下攻击键蓄力时强制终止连射然后开始蓄力
  _手动蓄力(key){
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    if (this.aFlag) {
      this._up(1)
      this.aFlag := False
    }
    this._down(1)
    while (MyFuns.getVkState(vk)) {
      sleep(20)
    }
    this._up(1)
  }
  ;各机体的蓄力
  ;YuanNang
  _蓄力950(key){
    this._蓄力攻击(950, key)
  }
  ;Ash-Marion
  _蓄力1850(key){
    this._蓄力攻击(1850, key)
  }
  ;Valnus
  _蓄力1700(key){
    this._蓄力攻击(1700, key)
  }
  ;Tetsu
  _蓄力2050(key){
    this._蓄力攻击(2050, key)
  }
  ;轻微位移用
  _右微移(key){
    MyFuns.press("right", 50)
    sleep(50)
  }
  _左微移(key){
    MyFuns.press("left", 50)
    sleep(50)
  }
  ;fbneo64的窗口变换,整个窗体长宽比大概是0.7
  _FBNEO64窗口变换(key){
    static W_TITLE := "ahk_exe fbneo64.exe"
    static HEIGHT := 768
    static WIDTH := Round(HEIGHT*0.7)
    static X:= Round((1366-WIDTH)/2)-100
    ;执行
    MyFuns.setWindow(W_TITLE, WIDTH, HEIGHT, X, 0)
  }
  ;游聚屏幕变换
  _游聚Tab屏变换(key){  
    static W_TITLE := "(setycyas) ahk_exe x-zone.exe"
    ;自定的两边留空和标题栏宽度,窗口参数
    static SIDE := 20
    static TITLE_BAR := 76
    static HEIGHT := 768+(TITLE_BAR-40)
    static WIDTH := Round(768*0.7)+SIDE*2
    static X:= Round((1366-WIDTH)/2)-100
    ;执行
    MyFuns.setWindow(W_TITLE, WIDTH, HEIGHT, X, -(TITLE_BAR-40))
  }
  _游聚Tab屏还原(key){  
    static W_TITLE := "(setycyas) ahk_exe x-zone.exe"
    static HEIGHT := 768
    static WIDTH := Round(768*0.7)
    ;执行
    MyFuns.setWindow(W_TITLE, WIDTH, HEIGHT, 0, 0)
  }

  /* 重载宏接口
  */
  _setMacro(){
    static obm := ObjBindMethod ;函数名简写,制作对象绑定函数
    local m := OrderMap() ;this.macroMap的简写
    ;制作宏相关Map
    this.macroMap := m
    m["连发A"] := obm(this, "_连发A")
    m["手动蓄力"] := obm(this, "_手动蓄力")
    m["蓄力950"] := obm(this, "_蓄力950")
    m["蓄力1700"] := obm(this, "_蓄力1700")
    m["蓄力1850"] := obm(this, "_蓄力1850")
    m["蓄力2050"] := obm(this, "_蓄力2050")
    m["左微移"] := obm(this, "_左微移")
    m["右微移"] := obm(this, "_右微移")
    m["FBNEO64窗口变换"] := obm(this, "_FBNEO64窗口变换")
    m["游聚Tab屏变换"] := obm(this, "_游聚Tab屏变换")
    m["游聚Tab屏还原"] := obm(this, "_游聚Tab屏还原")
    m["空函数"] := obm(this, "_空函数")
    ;制作指定宏顺序的数组
    this.macroArr := m.getKeys()
  }
}

/* 制作武装飞鸟1专用UI
*/
class Gunbird1UI extends CommonUI {

  HOTKEY_NUM := 7
  EXE_ARRAY := [MyEmu.FBNEO64, MyEmu.MAME, MyEmu.XZONE]
  DRAW_WINDOW := DrawWindow() ;画窗口对象
  
  /* 构造函数
  */
  __New(macro){
    super.__New("武装飞鸟1", macro)
    local i, v
    this._preset := OrderMap()
    this._macro.UI := this ;绑定UI
    ;设置preset和提升蓄力宏的优先级
    this._preset["YuanNang"] := ["连发A", "蓄力950", "左微移", 
      "右微移", "游聚Tab屏变换", "游聚Tab屏还原", "手动蓄力"
    ]
    this._preset["Tetsu"] := ["连发A", "蓄力2050", "左微移", 
      "右微移", "游聚Tab屏变换", "游聚Tab屏还原", "手动蓄力"
    ]
    this._preset["Valnus"] := ["连发A", "蓄力1700", "左微移", 
      "右微移", "游聚Tab屏变换", "游聚Tab屏还原", "手动蓄力"
    ]
    this._preset["Ash-Marion"] := ["连发A", "蓄力1850", "左微移", 
      "右微移", "游聚Tab屏变换", "游聚Tab屏还原", "手动蓄力"
    ]
    this.MACRO_PRIORITY["蓄力950"] := "p1"
    this.MACRO_PRIORITY["蓄力1700"] := "p1"
    this.MACRO_PRIORITY["蓄力1850"] := "p1"
    this.MACRO_PRIORITY["蓄力2050"] := "p1"
    this.MACRO_PRIORITY["手动蓄力"] := "p1"
    this.dw_isActive := False ;画的窗口是否可见
  }

  /* 重写接口
  */
  ;自定义gui组件标签页,增加一个挡板召唤键
  _customGui(){
    super._customGui()
    ;增加召唤标尺窗口按键
    this._drawWindowButton := this._myGui.Add("Button", "Default w200", "召唤标尺")
    this._drawWindowButton.OnEvent(
      "Click", 
      ObjBindMethod(this, "_onDrawWindowButtonClick")
    )
    ;增加测试按键
    this._testButton := this._myGui.Add("Button", "Default w200", "测试")
    this._testButton.OnEvent(
      "Click", 
      ObjBindMethod(this, "_onTestButtonClick")
    )
  }

  /* 重写setup,更改可选的exe,读入预设
  */
  setup(){
    super.setup()
    this._myGui.opt("+AlwaysOnTop")
    this._myGui.show("x1100 y230")
    this.readPresetMenu("YuanNang")
  }
  
  /* 自定义函数
  */
  ;召唤标尺窗口
  _onDrawWindowButtonClick(btnObj, info){
    local x := this._macro.testParams[1]
    
    ;画线,如果还没画的话
    if (this.DRAW_WINDOW.drawCount() == 0) {
      this.DRAW_WINDOW.drawNew("左2", 359-x, 743, 2*x, 25, 0xaaaaaa)
      this.DRAW_WINDOW.drawNew("右2", 807-x, 743, 2*x, 25, 0xaaaaaa)
      this.DRAW_WINDOW.drawNew("左1", 490-x, 743, 2*x, 25, 0x0000ff)
      this.DRAW_WINDOW.drawNew("右1", 677-x, 743, 2*x, 25, 0x0000ff)
      this.DRAW_WINDOW.drawNew("中线", 583-x, 743, 2*x, 25, 0xff00ff)
      this.dw_isActive := True
      return
    }
    
    ;已画线的话只需要切换
    if this.dw_isActive {
      this.DRAW_WINDOW.hideAll()
      this.dw_isActive := False
    } else {
      this.DRAW_WINDOW.showAll()
      this.dw_isActive := True
    }
  }

  _onTestButtonClick(btnObj, info){ 
    ;this.DRAW_WINDOW.changeWindow("中线", this._macro.testParams[1], 748, 10, 20)     
  }
}

/* 运行当前脚本
*/ 
if (A_ScriptName == "武装飞鸟1.2.ahk") {
  ;iconFile := "..\Icon\怒首领蜂最大往生_光头像01.jpg"
  ;TraySetIcon(iconFile, , 1)

  face := FaceDetector() ;方向检测对象
  macro := Gunbird1Macro(face, ["q","f"]) ;宏对象,有界面时第二个参数可以随便写但长度不要小于2
  ;新建界面对象并显示
  ui := Gunbird1UI(macro)
  ui.setup()
  ;更新组件内容
  ui._exeComboBox.Choose(MyEmu.XZONE)
  ;ui._exeComboBox.Choose(MyEmu.FBNEO64)
  ui._gameKeyEdit.Text := "q,space"
  ui._hotkeyEdit.Text := "~s,~d,~e,~f,~o,~l,~a"
  ui._fpsEdit.Text := 10
  ui._testEdit.Text := "2,20,30,30"
  ui.readPresetMenu("YuanNang")
}
